package p2pscrabble;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.thoughtworks.xstream.XStream;

public class GameState {
	private int turn;
	private char[][] boardLetter;
	private char[] player1Letter;
	private char[] player2Letter;
	private char[] bagLetter;
	private int bagCounter;
	private int swapCount;
	private int player1Score;
	private int player2Score;
	private boolean player1Turn;

	public GameState() {
		setTurn(0);
		boardLetter = new char[15][15];
		player1Letter = new char[7];
		player2Letter = new char[7];
		setBagLetter(new char[100]);
		setBagCounter(0);
		player1Score = 0;
		player2Score = 0;
		swapCount = 0;
		player1Turn = false;
		initArray();
	}
	
	public GameState(GameState g) {
		super();
		// copy all info
		setTurn(g.getTurn());
		this.boardLetter = new char[15][15];
		setBoardLetter(g.getBoardLetter());
		setPlayer1Letter(g.getPlayer1Letter());
		setPlayer2Letter(g.getPlayer2Letter());
		setBagLetter(g.getBagLetter());
		setBagCounter(g.getBagCounter());
		setPlayer1Score(g.getPlayer1Score());
		setPlayer2Score(g.getPlayer2Score());
		setSwapCount(g.getSwapCount());
		setPlayer1Turn(g.getPlayer1Turn());
	}

	private void initArray() {
		int i, j;
		for (i = 0; i < 15; i++) {
			for (j = 0; j < 15; j++) {
				boardLetter[i][j] = ' ';
			}
		}
		for (i = 0; i < 7; i++) {
			player1Letter[i] = '-';
			player2Letter[i] = '-';
		}
	}

	public char[][] getBoardLetter() {
		return boardLetter;
	}

	public void setBoardLetter(char[][] boardLetter) {
		for (int i = 0; i < 15; i++) {
			for (int j = 0; j < 15; j++) {
				this.boardLetter[i][j] = boardLetter[i][j];
			}
		}
	}

	public void setBoardLetter(int row, int col, char letter) {
		this.boardLetter[row][col] = letter;
	}

	public char[] getPlayer1Letter() {
		return player1Letter;
	}

	public void setPlayer1Letter(char[] myLetter) {
		this.player1Letter = myLetter;
	}

	public char[] getPlayer2Letter() {
		return player2Letter;
	}

	public void setPlayer2Letter(char[] oppLetter) {
		this.player2Letter = oppLetter;
	}

	public void setSwapCount(int swapCount) {
		this.swapCount = swapCount;
	}

	public int getSwapCount() {
		return swapCount;
	}

	public void setPlayer1Score(int score) {
		this.player1Score = score;
	}

	public int getPlayer1Score() {
		return player1Score;
	}

	public void setPlayer2Score(int score) {
		this.player2Score = score;
	}

	public int getPlayer2Score() {
		return player2Score;
	}

	public void setBagCounter(int bagCounter) {
		this.bagCounter = bagCounter;
	}

	public int getBagCounter() {
		return bagCounter;
	}

	public void setBagLetter(char[] bagLetter) {
		this.bagLetter = (char[]) bagLetter;
	}

	public char[] getBagLetter() {
		return bagLetter;
	}

	public void setPlayer1Turn(boolean b) {
		player1Turn = b;
	}

	public boolean getPlayer1Turn() {
		return player1Turn;
	}

	public void print() {
		System.out.println(turn);
		System.out.println(player1Letter);
		System.out.println(player2Letter);
		System.out.println(bagLetter);
		System.out.println("bagCounter = " + bagCounter);
		System.out.println("swapCount = " + swapCount);
		System.out.println("player1Score = " + player1Score);
		System.out.println("player2Score = " + player2Score);
		System.out.println("player1Turn = " + player1Turn);
	}

	public void writeIntoFile(String filename, boolean b) {
		try {
			int i = b ? 1 : 2;
			File f = new File(filename + i + ".xml");
			File f2 = new File(filename + i + "old.xml");

			// destroy old state
			if (f.exists()) {
				if (f2.exists()) {
					f2.delete();
				}
				copyfile(f, f2);
			}

			FileWriter fstream = new FileWriter(f, false);
			BufferedWriter out = new BufferedWriter(fstream);

			XStream xs = new XStream();
			
			out.write(xs.toXML(this));

			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static void copyfile(File f1, File f2) {
		try {
			InputStream in = new FileInputStream(f1);
			// For Overwrite the file.
			OutputStream out = new FileOutputStream(f2);

			byte[] buf = new byte[1024];
			int len;
			while ((len = in.read(buf)) > 0) {
				out.write(buf, 0, len);
			}

			in.close();
			out.close();
		}

		catch (FileNotFoundException ex) {
			System.out
					.println(ex.getMessage() + " in the specified directory.");
		}

		catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}

	public String toXML() {
		XStream xs = new XStream();
		return xs.toXML(this);
	}

	public void setTurn(int turn) {
		this.turn = turn;
	}

	public int getTurn() {
		return turn;
	}

	public void updateState(String s, int counter) {
		turn++;

		String[] split = s.split(";");

		// my letter & opp letter
		if (!split[0].equals(" "))
			setPlayer1Letter(split[0].toCharArray());
		if (!split[1].equals(" "))
			setPlayer2Letter(split[1].toCharArray());

		// bag letter
		if (!split[2].equals(" "))
			setBagLetter(split[2].toCharArray());

		// bag counter
		if (split[3].equals(" "))
			setBagCounter(counter);
		else
			setBagCounter(Integer.parseInt(split[3]));

		// my score & opp score
		setPlayer1Score(getPlayer1Score() + Integer.parseInt(split[4]));
		setPlayer2Score(getPlayer2Score() + Integer.parseInt(split[5]));

		// my turn
		setPlayer1Turn(Boolean.parseBoolean(split[6]));
	}
}
